package cn.nawang.ebeim.client.version;

import java.util.List;

import cn.nawang.ebeim.client.bean.DbVersionTreeBean;

public class FileNode implements Node {

  private static final long serialVersionUID = 1L;
  private String nid;
  private String pid;
  private String name;
  private Node parent;
  private byte status = Node.STATUS_NOOP;
  private FileInfo info = new FileInfo();

  public String getPid() {
    return pid;
  }

  public void setPid(String pid) {
    this.pid = pid;
  }

  public FileNode() {
    super();
  }

  public FileNode(String name) {
    super();
    this.name = name;
  }

  public String getNid() {
    return nid;
  }

  public void setNid(String nid) {
    this.nid = nid;
  }

  @Override
  public void visit() {
    VersionTree.count++;
    String path = "";
    if (parent != null) {
      path = parent.getName() + "/";
    }
    System.out.println(path + name + "\t" + getStatus()+"\t"+getNid()+"\t"+getPid());
  }
  
  @Override
  public void addToList(String dsId,String version,List<DbVersionTreeBean> beans) {
    
    DbVersionTreeBean treeBean = new DbVersionTreeBean();
    treeBean.setDsId(dsId);
    treeBean.setId(getNid());
    treeBean.setPid(getPid());
    treeBean.setName(getName());
    treeBean.setStatus(getStatus());
    treeBean.setVersion(version);
    beans.add(treeBean);
  };

  @Override
  public boolean traverse(NodeOp op) {
    if(getStatus()!=FileNode.STATUS_BEFORE_DELETED && getStatus()!=FileNode.STATUS_DELETED){
      return op.doTask(this);
    }
    return true;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Node getParent() {
    return parent;
  }

  public void setParent(Node parent) {
    this.parent = parent;
  }
  
  
  public void setDeleteStatus(byte status){
    this.setStatus(status);
  }
  
  public void mergeAll(Node node){
//    node.setStatus(Node.STATUS_DELETED);
    if(this.getParent()!=null){
      String newSign = this.getInfo().getSignature();
      String oldSign = ((FileNode)node).getInfo().getSignature();
      if(!newSign.equals(oldSign)){
        if(status != Node.STATUS_FAILED){
          this.setStatus(Node.STATUS_CHANGE);
        }
      }else{
        setStatus(Node.STATUS_NO_CHANGE);
      }
    }
  }
  @Override
  public void merge(Node node) {
    FileNode fNode = (FileNode) node;
    if (info.getSignature().equals(fNode.getInfo().getSignature())) {
      setStatus(Node.STATUS_NO_CHANGE);
    } else {
      setStatus(Node.STATUS_CHANGE);
      this.info = fNode.getInfo();
    }
    System.out.println("merge node:" + node.getName());

  }

  @Override
  public byte getStatus() {
    return status;
  }

  @Override
  public void setStatus(byte status) {
    this.status = status;
  }

  public FileInfo getInfo() {
    return info;
  }

  public void setInfo(FileInfo info) {
    this.info = info;
  }

  @Override
  public boolean filterChanges(List<Node> list) {
    if (status != STATUS_NO_CHANGE) {
      list.add(this);
      return true;
    }
    return false;
  }

  @Override
  public String toString() {
    return "FileNode [name=" + name + ", parent=" + parent + ", status=" + status + ", info=" + info
        + "]";
  }

  @Override
  public boolean filter(List<Node> list, Filter filter) {
    if (filter.filter(this)) {
      list.add(this);
    }
    return true;
  }

  @Override
  public String getPath() {
    if (getParent() != null) {
      return getParent().getPath();
    }
    return "";
  }

}
